package com.rew.canteenbox.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.rew.canteenbox.R;
import com.rew.canteenbox.activities.InfoActivity;
import com.rew.canteenbox.adapter.BuyListAdapter;
import com.rew.canteenbox.bases.AppData;
import com.rew.canteenbox.models.Orders;

import java.util.List;

/**
 *  Fragment Buy.
 *
 * Created by HuiWen Ren on 2016/11/10.
 */
public class FBuy extends Fragment{

    private View view;
    private AppData appData;
    private BuyListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_buy, container,false);
        appData = (AppData) getActivity().getApplication();
        init();
        return view;
    }

    private void init() {
        List<Orders> ordersList = appData.getOrdersList();
        if(ordersList.isEmpty()){
            view.findViewById(R.id.ll_buy_none).setVisibility(View.VISIBLE);
        } else {
            view.findViewById(R.id.ll_buy_none).setVisibility(View.GONE);
            adapter = new BuyListAdapter(getActivity(),
                    R.layout.item_order, ordersList);
            ListView listView = (ListView) view.findViewById(R.id.buy_list);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener((parent, view, position, id) -> {
                Orders orders = ordersList.get(position);
                InfoActivity.actionStart(getActivity(), orders.getDishes().getDishName());
            });
        }

    }
}
